﻿using Knight.Web.Infrastructure;
using Knight.Web.Infrastructure.Menus;
using Knight.Models.Base;
using Knight.Models.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Areas.Manage.Controllers.Filters;

namespace WebApp.Areas.Manage.Controllers
{
    public class SysSettingController : BaseAdminController
    {

        #region 上传文件
        const string AdUploadDirectory = "pic";    //由于"ad"会被一些广告拦截软件拦截，所以此处改为pic
        /// <summary>
        /// 上传图片
        /// </summary>
        [HttpPost]
        public JsonResult UploadPics()
        {
            var rsp = this.UploadPics(AdUploadDirectory);
            return EJItemJson(rsp);
        }

        [HttpPost]
        public JsonResult UploadIco()
        {
            var rsp = this.Upload(AdUploadDirectory);
            return EJItemJson(rsp);
        }
        #endregion

        [MallAdminMenu("Web_SiteGlobal")]
        [Permission("Web_SiteGlobal")]
        public ActionResult Website()
        {
            ViewBag.Setting = new DbSettingApi().GetSertting<SysSetting>();
            return View();
        }

        [HttpPost]
        public JsonResult Website_Save(SysSetting model, string tempImageUrl, string tempImageUrl1)
        {
            model.Logo = UploadHelper.MoveFile(tempImageUrl);
            model.ICO = UploadHelper.MoveFile(tempImageUrl1);
            return Save(model);
        }

        JsonResult Save(SettingBase setting)
        {
            try
            {
                var result = new DbSettingApi().SaveSetting(setting);
                return EJActionJson(result);
            }
            catch (Exception ex)
            {
                return EJActionJson(false, "异常", ex.Message);
            }
        }
    }
}